const swap = function (nums, i, j) {
  let temp = nums[i]
  nums[i] = nums[j]
  nums[j] = temp
}
const removeElement1 = function (nums, val) {
  let slow = 0
  let fast = 0
  while (fast < nums.length) {
    if (nums[fast] !== val) {
      fast++
      slow++
    } else {
      slow = fast
      while (nums[fast] === val) {
        fast++
      }
      if (fast >= nums.length) break
      swap(nums, slow, fast)
      slow++
      fast = slow
    }
  }
  return slow
}

const removeElement2 = function (nums, val) {
  let slow = 0
  let fast = 0
  while (fast < nums.length) {
    if (nums[fast] !== val) {
      nums[slow] = nums[fast]
      slow++
    }
    fast++
  }
  return slow
}

let nums = [0, 1, 2, 2, 3, 0, 4, 2]
removeElement2(nums, 2)

module.exports = {
  removeElement2
}
